$(function()
{
	var addImgItem = function(btn)
	{
		var thisParent = $(btn).closest('.upload-gallery-wrap-outer');
		var clone = thisParent.clone(true);
		var name = $('input[type="file"]', thisParent).attr('name').replace('[]', '');
		var num = parseInt($('#' + name).val());

		if (num > 1)
		{
			$('input[type="file"]', clone).val('');
			$('.uneditable-input', clone).text(matrix.yp.click_here_to_upload);
			$('.upload-title', clone).val('');
			thisParent.after(clone);
			$('#' + name).val(num - 1);
		}
		else
		{
			matrix.notifFloatBox({msg: matrix.yp.no_more_files, type: 'notif', autohide: true, pause: 5000});
		}

		detectFilename();
	};

	var removeImgItem = function(btn)
	{
		var thisParent = $(btn).closest('.upload-gallery-wrap-outer');
		var name = $('input[type="file"]', thisParent).attr('name').replace('[]', '');
		var num = parseInt($('#' + name).val());

		if (thisParent.prev().hasClass('upload-gallery-wrap-outer') || thisParent.next().hasClass('upload-gallery-wrap-outer'))
		{
			thisParent.remove();
			$('#' + name).val(num + 1);
		}
	};

	$('.js-add-img').on('click', function(e)
	{
		e.preventDefault();
		addImgItem(this);
	});

	$('.js-remove-img').on('click', function(e)
	{
		e.preventDefault();
		removeImgItem(this);
	});
});
